#!/bin/bash

echo "Building JSZ Language Support extension..."

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo "Error: Node.js not found, please install Node.js first"
    exit 1
fi

# Check if npm is installed
if ! command -v npm &> /dev/null; then
    echo "Error: npm not found, please ensure Node.js is properly installed"
    exit 1
fi

# Install dependencies
echo "Installing dependencies..."
npm install
if [ $? -ne 0 ]; then
    echo "Error: Failed to install dependencies"
    exit 1
fi

# Check if vsce is installed
if ! command -v vsce &> /dev/null; then
    echo "Installing vsce..."
    npm install -g vsce
    if [ $? -ne 0 ]; then
        echo "Error: Failed to install vsce"
        exit 1
    fi
fi

# Package extension
echo "Packaging extension..."
vsce package
if [ $? -ne 0 ]; then
    echo "Error: Failed to package extension"
    exit 1
fi

echo ""
echo "✅ JSZ Language Support extension built successfully!"
echo ""
echo "The generated .vsix file can be installed in VS Code:"
echo "1. Open VS Code"
echo "2. Press Ctrl+Shift+P to open command palette"
echo "3. Type \"Extensions: Install from VSIX...\""
echo "4. Select the generated .vsix file"
echo ""